package cn.qayy.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;

import cn.qayy.constants.Constant;
import cn.qayy.service.IBasisService;
import cn.qayy.service.IBidService;
import cn.qayy.service.IPlanService;
import cn.qayy.service.IPolicyService;
import cn.qayy.service.IRegisterService;
import cn.qayy.service.IStandardService;
import cn.qayy.utils.ServletContextUtils;
/***
 * 系统维护
 * @author jobs1127
 *
 */
@Controller
@RequestMapping(value = "/system")
@SuppressWarnings("all")
public class SystemController {
	// 注解加载查询sevice层
	/**流向接口*/
	@Resource
	private IPlanService planService;

	/**注册库接口*/
	@Resource
	private IRegisterService registerService;
	
	/**基础库接口*/
	@Resource
	private IBasisService iBasisService;
	
	/**中标库接口*/
	@Resource
	private IBidService iBidService;
	
	/**标准测算价接口*/
	@Resource
	private IStandardService iStandardService;
	
	/**协议表接口*/
	@Resource
	private IPolicyService iPolicyService;
	/***
	 * 刷新系统上下文缓存
	 * @return
	 */
	@RequestMapping(value = "/refreshContext")
	public String refreshContext() {
		/***
    	 * 获取项目上下文的共享数据
    	 */
    	ServletContext context = ServletContextUtils.getServletContext();
    	try {
			long startTime = System.currentTimeMillis();
			Set<String> commercial1List = new HashSet<>(Constant.SMALL_CAPACITY);
			Set<String> terminalList = new HashSet<>(Constant.SMALL_CAPACITY);
			Set<String> mgrList = new HashSet<>(Constant.SMALL_CAPACITY);
			Set<String> qsTerminalList = new HashSet<>(Constant.SMALL_CAPACITY);
			Set<String> fenbuList = new HashSet<>(Constant.SMALL_CAPACITY);
			Set<String> daibiaoList = new HashSet<>(Constant.SMALL_CAPACITY);
			Set<String> tichengbelongList = new HashSet<>(Constant.SMALL_CAPACITY);
			Set<String> clientList = new HashSet<>(Constant.SMALL_CAPACITY);
			HashMap<String, Object> condiMap = new HashMap<String, Object>(Constant.INIT_CAPACITY*2);
			
			List<Map<String, String>> condiList = new ArrayList<Map<String, String>>(Constant.CENTER_CAPACITY);
			List<Map<String, String>> jiafanAndYifans = new ArrayList<Map<String, String>>(Constant.CENTER_CAPACITY);
			/**省份、地市、区县*/
			List<Map<String, String>> pccList = new ArrayList<Map<String, String>>(Constant.SMALL_CAPACITY*4);
			/**基础库 省份 */
			Set<String> provinces = new HashSet<String>(Constant.SMALL_CAPACITY*4);
			/**基础库 地市 */
			Set<String> citys = new HashSet<String>(Constant.SMALL_CAPACITY*4);
			/**基础库 区县*/
			Set<String> countrys = new HashSet<String>(Constant.SMALL_CAPACITY*4);
			/**基础库判断重复的key*/
			Set<String> basicKeys = new HashSet<String>(Constant.SMALL_CAPACITY*8);
			/**协议标准测算价判断重复项的key*/
			Set<String> pbKeys = new HashSet<String>(Constant.SMALL_CAPACITY*2);
			/**中标价判断重复项的key*/
			Set<String> pzKeys = new HashSet<String>(Constant.SMALL_CAPACITY*2);
			/**协议表判断重复项的key*/
			Set<String> policyKeys = new HashSet<String>(Constant.CENTER_CAPACITY);
			
			
			jiafanAndYifans.addAll(registerService.selectAllJiafanAndYifan());
			basicKeys.addAll(iBasisService.selectBasicKeys());
			pbKeys.addAll(iStandardService.selectPbKeys());
			pzKeys.addAll(iBidService.selectPzKeys());
			policyKeys.addAll(iPolicyService.selectPolicyKeys());
			pccList.addAll(iBasisService.selectAllProvincesAndCityAndCountry());
			for (Map<String, String> map : pccList) {
				provinces.add(map.get("province"));
				citys.add(map.get("city"));
				countrys.add(map.get("country"));
			}
			
			/***注册库 甲方/乙方 **/
			Set<String> cNames = new HashSet<String>(Constant.CENTER_CAPACITY);
			/**注册库 Qs名 ***/
			Set<String> qsNames = new HashSet<String>(Constant.CENTER_CAPACITY);
			for (Map<String, String> map : jiafanAndYifans) {
				cNames.add(map.get("cname"));
				qsNames.add(map.get("qsname"));
			}
			
			condiList.addAll(planService.selectGroup());
			for (Map<String, String> map : condiList) {
				mgrList.add(map.get("mgr"));
				terminalList.add(map.get("terminal"));
				commercial1List.add(map.get("commercial1"));
				fenbuList.add(map.get("fenbu"));
				qsTerminalList.add(map.get("zhenghe_qs"));	//流向QS名
				daibiaoList.add(map.get("daibiao"));
				tichengbelongList.add(map.get("tichengbelong"));
				clientList.add(map.get("client"));
			}
			
			String resultJson = JSONObject.toJSONString(commercial1List);
			//出货甲方 json
			condiMap.put("commercial", resultJson);
			//入货乙方 json
			resultJson = JSONObject.toJSONString(cNames);
			condiMap.put("terminal", resultJson);
			resultJson = JSONObject.toJSONString(mgrList);
			condiMap.put("mgr", resultJson);
			resultJson = JSONObject.toJSONString(qsTerminalList);
			condiMap.put("qsterminal", resultJson);
			resultJson = JSONObject.toJSONString(fenbuList);
			condiMap.put("fenbu", resultJson);
			resultJson = JSONObject.toJSONString(daibiaoList);
			condiMap.put("daibiao", resultJson);
			resultJson = JSONObject.toJSONString(tichengbelongList);
			condiMap.put("tichengbelong", resultJson);
			resultJson = JSONObject.toJSONString(clientList);
			condiMap.put("client", resultJson);
			// 省地市多选
			List<Map<String, String>> listMap = new ArrayList<Map<String, String>>(Constant.SMALL_CAPACITY*8);
			listMap.addAll(registerService.selectCommercialSourceAuto());

			/***注册库片区 */
			Set<String> spaceList = new HashSet<>(Constant.SMALL_CAPACITY);
			/***注册库地市 */
			Set<String> cityList = new HashSet<>(Constant.SMALL_CAPACITY);
			/***注册库区县 */
			Set<String> countryList = new HashSet<>(Constant.SMALL_CAPACITY);
			/***注册库大区 */
			Set<String> provinceList = new HashSet<>(Constant.SMALL_CAPACITY);
			/***注册库省份 */
			Set<String> shengfenList = new HashSet<>(Constant.SMALL_CAPACITY);
			/***注册库入货乙方级别 */
			Set<String> terminallveList = new HashSet<>(Constant.SMALL_CAPACITY);
			
			for (Map<String, String> map : listMap) {
				spaceList.add(map.get("space"));
				terminallveList.add(map.get("terminallve"));
				cityList.add(map.get("city"));
				countryList.add(map.get("country"));
				provinceList.add(map.get("province"));
				shengfenList.add(map.get("shengfen"));
			}
			
			resultJson = JSONObject.toJSONString(cityList);
			condiMap.put("cityStr", resultJson);
			resultJson = JSONObject.toJSONString(countryList);
			condiMap.put("countryList", resultJson);
			condiMap.put("terminallve", terminallveList);
			condiMap.put("space", spaceList);
			condiMap.put("city", cityList);
			condiMap.put("country", countryList);
			condiMap.put("province", provinceList);
			condiMap.put("shengfen", shengfenList);
			
			long endTime = System.currentTimeMillis();
			System.out.println("初始化省地市区县耗时：" + (endTime - startTime)/1000);
			
			//注册库qs名
			resultJson = JSONObject.toJSONString(qsNames);
			context.setAttribute("qsNamesStr", resultJson);
			
			context.setAttribute("condiMap", condiMap);
			
			/***注册库所有已注册的甲方或乙方**/
			resultJson = JSONObject.toJSONString(cNames);
			context.setAttribute("cNames", cNames);
			/***注册库所有已注册的甲方或乙方对应的qs名**/
			context.setAttribute("qsNames", qsNames);
			
			/**基础库 省份 地市 区县 大区、片区、级别**/
			context.setAttribute("provinces",provinces);
			context.setAttribute("citys",citys);
			context.setAttribute("countrys",countrys);
			context.setAttribute("provinceList",provinceList);
			context.setAttribute("spaceList",spaceList);
			context.setAttribute("terminallveList",terminallveList);
			/***基础库中所有的不重复项keys 省份+地市+区县+分组标识 唯一对应一个经理项 **/
			context.setAttribute("basicKeys", basicKeys);
			
			/***协议标准测算价中所有的不重复项keys 省份+品种+包装+模式+条线+渠道  唯一对应一个标准测算价项 **/
			context.setAttribute("pbKeys", pbKeys);

			/***协议中标价中所有的不重复项keys 省份+地市+终端+品种+包装+开始时间+结束时间  唯一对应一个中标价项 **/
			context.setAttribute("pzKeys", pzKeys);
			
			/***协议中所有的不重复项keys 客户+品种+包装+入货乙方  唯一对应一个协议 **/
			context.setAttribute("policyKeys", policyKeys);
			
			System.out.println("刷新了系统上下文缓存");
		} catch(Exception e) {
			e.printStackTrace();
			return "index";
		}
		return "index";
	}
}
